//
//  XinZengZhiWeiVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/10.
//

#import "XinZengZhiWeiVC.h"
#import "CommonInsetInfoViewTableViewCell.h"
#import "ChoseLianXiRenHeaderView.h"
#import "ChoseMoRenPersonVC.h"
#import "BeiZhuFooterView.h"
#import "SiJiTypeView.h"
#import "LXChooseManagerViewController.h"
#import "FuZeRenModel.h"
@interface XinZengZhiWeiVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView       *tableView;
@property (nonatomic, strong) CommonChoseTypeRoleView  *choseTypeView;
@property (nonatomic, strong) NSMutableArray    *dataSource;
@property (nonatomic, strong) SiJiTypeView     *typeView;
@property (nonatomic, assign) CGFloat           collectionViewHeight;
@property (nonatomic, assign) NSInteger         selectIndex;
@property (nonatomic, strong) NSMutableArray    *salaryRangeArray;
@property (nonatomic, strong) BeiZhuFooterView  *beiZhuView ;
@property (nonatomic, strong) FuZeRenModel      *selectModel;
@property (nonatomic, strong) NSString          *jobName;//职位
@property (nonatomic, strong) NSString          *salaryRange;//薪资范围
@property (nonatomic, strong) NSString          *userLiaisonName;//联系人名称
@property (nonatomic, strong) NSString          *userLiaisonPhone;//联系人电话

@property (nonatomic, strong) NSString          *userName;//负责人姓名
@property (nonatomic, strong) NSString          *userNamePhone;//负责人电话
@property (nonatomic, strong) NSString          *userNameNickName;//负责人别名
@property (nonatomic, strong) NSString          *userNameRoleName;//负责人角色
@property (nonatomic, strong) NSString          *driverNature;//司机属性
@property (nonatomic, strong) NSString          *realName;
@property (nonatomic, strong) NSString          *remarks;//备注
@property (nonatomic, strong) NSMutableArray    *driverNatureArray;

@end

@implementation XinZengZhiWeiVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"职位新增"];
}
- (void)viewDidLoad{
    [super viewDidLoad];
     self.salaryRangeArray = [[NSMutableArray alloc] init];
     self.driverNatureArray = [[NSMutableArray alloc] init];
    [self getCommonArrayWithType:@"SJSX"];
     self.selectIndex = 0;

     self.dataSource = [NSMutableArray array];
     NSArray *oneArray = @[@"职位名称",@"薪资范围"];
     NSArray *twoArray = @[@"联系人",@"负责人"];
    [self.dataSource addObject:oneArray];
    [self.dataSource addObject:twoArray];
     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, B_HEIGHT - LL_StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
     self.tableView.backgroundColor = White_Color;
     self.tableView.separatorColor = White_Color;
    [self.view addSubview:self.tableView];
   if(@available(iOS 11.0, *)){
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
   [self.tableView registerNib:[UINib nibWithNibName:[CommonInsetInfoViewTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CommonInsetInfoViewTableViewCell reuseIdentifier]];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommonInsetInfoViewTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:[CommonInsetInfoViewTableViewCell reuseIdentifier]];
    if (!cell){
        cell = [[CommonInsetInfoViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CommonInsetInfoViewTableViewCell reuseIdentifier]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.xingLb.hidden = YES;
    NSArray *cacheArray = self.dataSource[indexPath.section];
    cell.nameLb.text = cacheArray[indexPath.row];
    cell.nameLb.textColor = Black_Color;
    [cell setCurrentValueBlock:^(NSString * _Nonnull contentString, NSIndexPath * _Nonnull indexPath) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                self.jobName=contentString;
            }
        }
    }];
    if (indexPath.section == 0){
        cell.iconImage.hidden = YES;
        if (indexPath.row == 0){
            cell.contentLb.userInteractionEnabled = YES;
//            cell.nameLb.text=@"职位名称";
            if ([NSString isNUllString:self.jobName]){
               
                cell.contentLb.placeholder = @"请输入职位";
                cell.contentLb.text = @"";
            } else {
                cell.contentLb.text = self.jobName;
            }

        }else {
            cell.iconImage.hidden = NO;
            cell.contentLb.userInteractionEnabled = NO;
//            cell.nameLb.text=@"薪资范围";
            if ([NSString isNUllString:self.salaryRange]){
                cell.contentLb.placeholder = @"请选择薪资范围";
                cell.contentLb.text = @"";
            } else {
                cell.contentLb.text = self.salaryRange;
            }
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.iconImage.hidden = NO;
            cell.contentLb.userInteractionEnabled=NO;
            if ([NSString isNUllString:self.userLiaisonName]) {
                cell.contentLb.placeholder = @"请选择联系人";
            } else {
                cell.contentLb.text = [NSString stringWithFormat:@"%@-%@",self.userLiaisonName,self.userLiaisonPhone];
            }
        } else {
            cell.iconImage.hidden = NO;
            cell.contentLb.userInteractionEnabled = NO;
            if ([NSString isNUllString:self.userNameNickName]){
                cell.contentLb.placeholder = @"请选择负责人";
                cell.contentLb.text = @"";
            } else {
                cell.contentLb.text = [NSString stringWithFormat:@"%@",self.userNameNickName];
            }
        }
    }
    return cell;
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [CommonInsetInfoViewTableViewCell cellHeight];
 }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BD_WEAKSELF
    if (indexPath.section == 0 && indexPath.row == 1) {
        //薪资范围
        [weakSelf.view endEditing:YES];
        UIWindow  *currentWindow = [BaseViewController getCurrentWindow];
       if(weakSelf.choseTypeView){
          [weakSelf.choseTypeView removeFromSuperview];
        }
        weakSelf.choseTypeView = [CommonChoseTypeRoleView  shareInstance];
        weakSelf.choseTypeView.frame = CGRectMake(0, 0, ScreenWidth, B_HEIGHT);
        [weakSelf.choseTypeView DataSourceToShowAlert:weakSelf.salaryRangeArray withSelectTitle:weakSelf.salaryRange withType:@"1" withTitle:@"请薪资范围"];
      
       [currentWindow addSubview:weakSelf.choseTypeView];
       [currentWindow bringSubviewToFront:weakSelf.choseTypeView];
       [weakSelf.choseTypeView setToSendValue:^(JueSeModel * _Nonnull model) {
            weakSelf.salaryRange = model.value;
           [weakSelf.tableView reloadData];
           [weakSelf.choseTypeView removeFromSuperview];
       }];
       [weakSelf.choseTypeView.tableView reloadData];
  
    }else {
        if (indexPath.row == 0) {
            ChoseMoRenPersonVC *VC =[[ChoseMoRenPersonVC alloc] init];
            [VC setToSendValue:^(MoRenLianXiRenModel * _Nonnull model) {
                self.userLiaisonName = model.name;
                self.userLiaisonPhone = model.telePhone;
                [self.tableView reloadData];
            }];
            [self pushToNextController:VC];
        } else {
            //选择负责人
            LXChooseManagerViewController *vc = [[LXChooseManagerViewController alloc] init];
            vc.model = self.selectModel;
            [vc setToSendValue:^(FuZeRenModel * _Nonnull model) {
                self.selectModel = model;
                self.userName = model.realName;
                self.userNamePhone = model.userPhone;
                self.userNameNickName = model.realName;
                self.userNameRoleName = model.roleName;
                [self.tableView reloadData];
            }];
            
            [self pushToNextController:vc];
            

        }
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1)];
//        ChoseLianXiRenHeaderView  *  typeHeader=[ChoseLianXiRenHeaderView shareInstance];
//        typeHeader.frame=CGRectMake(0, 0, ScreenWidth, 44);
//        typeHeader.xingLb.hidden=YES;
//        typeHeader.nameLb.textColor=Black_Color;
//        [typeHeader.choseButton whenTapped:^{
//            ChoseMoRenPersonVC  *  VC =[[ChoseMoRenPersonVC alloc] init];
//            [VC setToSendValue:^(MoRenLianXiRenModel * _Nonnull model) {
//                self.userLiaisonName=model.name;
//                self.userLiaisonPhone=model.telePhone;
//                [self.tableView reloadData];
//            }];
//            [self pushToNextController:VC];
//        }];
//        [headerView addSubview:typeHeader];
//         typeHeader.nameLb.text=@"联系人";
//         typeHeader.choseButton.hidden=NO;
    return headerView;
    } else {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0)];
        return headerView;
    }
      
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    BD_WEAKSELF
    if (section == 1) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
        self.typeView = [SiJiTypeView shareInstance];
        [self.typeView setDataSource:self.driverNatureArray];
        [self.typeView currentSelectIndex:self.selectIndex];
        self.typeView.frame = CGRectMake(0, 0, ScreenWidth, self.typeView.collectionViewHeight);
        footerView.frame = CGRectMake(0, 0, ScreenWidth, 300+self.typeView.collectionViewHeight);
        [self.typeView setToSelectIndex:^(NSInteger selectIndex) {
            weakSelf.selectIndex = selectIndex;
            JueSeModel *model = weakSelf.driverNatureArray[selectIndex];
            weakSelf.driverNature = model.value;
            [weakSelf.typeView currentSelectIndex:weakSelf.selectIndex];
        }];
        [footerView addSubview:self.typeView];
        
         self.beiZhuView = [BeiZhuFooterView shareInstance];
         self.beiZhuView.frame = CGRectMake(0, self.typeView.collectionViewHeight, ScreenWidth, 150);
         self.beiZhuView.nameLb.text = @"详情描述";
         self.beiZhuView.nameLb.textColor = Black_Color;
        if (![NSString isNUllString:self.remarks]){
            self.beiZhuView.placeView.text = self.remarks;
        }
        [self.beiZhuView.placeView setDidChangeText:^(PlaceholderTextView *textView) {
             self.remarks = textView.text;
        }];
        [footerView addSubview:self.beiZhuView];
        
        UIButton *commitButton =[UIButton buttonWithType:UIButtonTypeCustom];
        commitButton.frame = CGRectMake(16, self.typeView.collectionViewHeight + 200, ScreenWidth-32, 42);
        commitButton.backgroundColor = mainColor;
        commitButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
        [commitButton setTitleColor:White_Color forState:UIControlStateNormal];
        [commitButton createBordersWithColor:nil withCornerRadius:5.0 andWidth:0];
        [commitButton setTitle:@"确定" forState:UIControlStateNormal];
        [commitButton addTarget:self action:@selector(commitButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:commitButton];
        return footerView;
    } else {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1)];
        return footerView;
    }
}
-(void)commitButtonClick{
    [self commitInfo];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 300 + [self getCollectionViewHeight];
    } else {
        return 0.1;
    }
}
-(CGFloat)getCollectionViewHeight{
   CGFloat photoWidth = 30;
    _collectionViewHeight = 0;
    _collectionViewHeight = _collectionViewHeight+20;
    NSInteger number = self.driverNatureArray.count%3;
    NSInteger zomeNumber = self.driverNatureArray.count/3;
    if (number == 0){
        number = zomeNumber;
    } else {
        number = zomeNumber+1;
    }
    _collectionViewHeight = _collectionViewHeight + (number * photoWidth);
    return _collectionViewHeight;
}

-(void)commitInfo{
    [self.view endEditing:YES];
  
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.jobName.length == 0) {
        showAlert(@"职位名称不能为空");
        return;
    }
    [params setObject:self.jobName forKey:@"jobName"];
   
    if (self.salaryRange.length == 0) {
        showAlert(@"请选择薪资范围");
        return;
    }
    [params setObject:self.salaryRange forKey:@"salaryRange"];

    if (self.userLiaisonName.length == 0) {
        showAlert(@"请选择联系人");
        return;
    }
    [params setObject:self.userLiaisonName forKey:@"userLiaisonName"];

    if (self.userLiaisonPhone.length == 0) {
        showAlert(@"请选择联系人");
        return;
    }
    [params setObject:self.userLiaisonPhone forKey:@"userLiaisonPhone"];
    if (self.userName.length == 0) {
        showAlert(@"请选择负责人");
        return;
    }
    
    [params setObject:self.userName forKey:@"userName"];
    [params setObject:self.userNamePhone forKey:@"userNamePhone"];
    [params setObject:self.userNameNickName forKey:@"userNameNickName"];
    [params setObject:self.userNameRoleName forKey:@"userNameRoleName"];
    if (self.driverNature.length == 0) {
        showAlert(@"请选择司机属性");
        return;
    }

    [params setObject:self.driverNature forKey:@"driverNature"];
    if (self.remarks.length == 0) {
        showAlert(@"请输入详情描述");
        return;
    }
    if (self.remarks.length > 200) {
        showAlert(@"详情描述不能超过200字");
        return;
    }
    [params setObject:self.remarks forKey:@"remarks"];
    
    
    
    [MSNetworkManager postRevertUrl:Add_Job_Url params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            showAlert(@"职位新增成功");
            [self.navigationController popViewControllerAnimated:YES];
        }

    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:NO];
    

}
-(void)getCommonArrayWithType:(NSString *)typeString{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    [params setObject:typeString forKey:@"code"];
    NSString *url = [NSString stringWithFormat:@"%@?code=%@",CarDetailDict,typeString];
    [MSNetworkManager postRevertUrl:url params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            NSArray *cacheArray = resonseObj;
            if ([typeString isEqualToString:@"XZFW"]){
                //薪资范围
                self.salaryRangeArray = [JueSeModel mj_objectArrayWithKeyValuesArray:cacheArray];
                [self.tableView reloadData];
            } else {
                //司机属性
                self.driverNatureArray = [JueSeModel  mj_objectArrayWithKeyValuesArray:cacheArray];
                self.selectIndex = 0;
                JueSeModel *model = self.driverNatureArray[0];
                self.driverNature = model.value;
                [self getCommonArrayWithType:@"XZFW"];
            }
           
        }

    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:NO];
    
   
}

@end
